package com.ctgu.computer.service.impl;

import com.ctgu.computer.common.UserLocalTread;
import com.ctgu.computer.domain.dto.NoticeAddDTO;
import com.ctgu.computer.domain.dto.NoticePageDTO;
import com.ctgu.computer.domain.po.AdminInfo;
import com.ctgu.computer.domain.po.ChannelInfo;
import com.ctgu.computer.domain.po.NoticeInfo;
import com.ctgu.computer.domain.result.PageResult;
import com.ctgu.computer.domain.vo.NoticeVO;
import com.ctgu.computer.mapper.AdminMapper;
import com.ctgu.computer.mapper.NoticeMapper;
import com.ctgu.computer.service.NoticeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NoticeServiceImpl implements NoticeService {

    private final NoticeMapper noticeMapper;
    private final AdminMapper adminMapper;
    @Override
    @Transactional
    public void addNotice(NoticeAddDTO noticeAddDTO) {
        Integer adminId;
        //获取当前管理员的id
        adminId = UserLocalTread.getAdminId();

        NoticeInfo noticeInfo = new NoticeInfo();

        noticeInfo = noticeInfo.builder()
                .adminId(adminId)
                .noticeId(noticeAddDTO.getNoticeId())
                .notice(noticeAddDTO.getNotice())
                .adminName(noticeAddDTO.getAdminName())
                .build();
        noticeMapper.insert(noticeInfo);

    }

    @Override
    public PageResult page(NoticePageDTO noticePageDTO) {
        PageHelper.startPage(noticePageDTO.getPageIndex(),noticePageDTO.getPageSize());
        Page<NoticeVO> res = noticeMapper.page(noticePageDTO);
        System.out.println("查询到的公告内容"+res.getResult());

        return new PageResult(res.getTotal(),res.getResult());
    }

    @Override
    public List<NoticeVO> getById(Integer adminId) {
        List<NoticeInfo> list = noticeMapper.selectByAdminId(adminId);
        List<NoticeVO> listVo = new ArrayList<>();

        for(NoticeInfo noticeInfo:list){
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setNoticeId(noticeInfo.getNoticeId());
            noticeVO.setAdminId(noticeInfo.getAdminId());
            noticeVO.setNotice(noticeInfo.getNotice());
            listVo.add(noticeVO);
        }

        return listVo;
    }

    @Override
    public void removeById(Integer noticeId) {
        noticeMapper.deleteByNoticeId(noticeId);

    }

    @Override
    public void updateById(NoticeAddDTO noticeAddDTO) {
        noticeMapper.updateById(noticeAddDTO);
    }

}
